package cc.booku.owlbox.LayPage;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.concurrent.Executors;

import cc.booku.owlbox.CollectionStation_norm.AsyncTaskImageLoad;
import cc.booku.owlbox.CollectionStation_norm.CollectionStation_C;
import cc.booku.owlbox.R;

public class LayClass_A extends BaseAdapter implements ListAdapter {


    private ArrayList<LayClass_C> layClassCArrayList;
    private int id;
    private Context context;
    private LayoutInflater inflater;

    public LayClass_A(int item, Context context, ArrayList<LayClass_C> layClassCArrayList) {
        this.layClassCArrayList = layClassCArrayList;
        this.context = context;
        this.id = item;
        inflater = LayoutInflater.from(context);

    }

    @Override
    public int getCount() {
        return layClassCArrayList.size();
    }

    @Override
    public Object getItem(int i) {
        return layClassCArrayList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }


    @SuppressLint("WrongConstant")
    @Override

    public View getView(final int i, View view, ViewGroup viewGroup) {


        TextView vod_score = null;
        TextView vod_name = null;
        TextView vod_class = null;
        TextView vod_add_time = null;
        ImageView vod_pic = null;
        TextView vod_play_url = null;
        TextView vod_pic_url = null;
        TextView vod_value_add = null;

        LayClass_A.ViewHolder viewHolder;
        if (view == null) {
            view = inflater.inflate(id, null);

            vod_score = (TextView) view.findViewById(R.id.vod_score);
            vod_name = (TextView) view.findViewById(R.id.vod_name);
            vod_class = (TextView) view.findViewById(R.id.vod_class);
            vod_add_time = (TextView) view.findViewById(R.id.vod_add_time);
            vod_value_add = (TextView) view.findViewById(R.id.vod_value_add);
            vod_play_url = (TextView) view.findViewById(R.id.vod_play_url);
            vod_pic_url = (TextView) view.findViewById(R.id.vod_pic_url);
            vod_pic = (ImageView) view.findViewById(R.id.vod_pic);


            view.setTag(new ViewHolder(vod_score, vod_name, vod_class, vod_add_time, vod_value_add, vod_play_url, vod_pic_url, vod_pic));
        } else {
            LayClass_A.ViewHolder viewHolder1 = (LayClass_A.ViewHolder) view.getTag(); // 重新获取ViewHolder
            vod_score = viewHolder1.vod_score;
            vod_class = viewHolder1.vod_class;
            vod_name = viewHolder1.vod_name;
            vod_add_time = viewHolder1.vod_add_time;
            vod_value_add = viewHolder1.vod_value_add;
            vod_play_url = viewHolder1.vod_play_url;
            vod_pic_url = viewHolder1.vod_pic_url;

        }
        LayClass_C collectionStation_c = (LayClass_C) layClassCArrayList.get(i); // 获取当前项的实例

        //video_public_classListc
        vod_name.setText(collectionStation_c.getVod_name().toString());//对象为空
        vod_class.setText(collectionStation_c.getVod_class().toString());
        vod_score.setText(collectionStation_c.getVod_score().toString());
        vod_add_time.setText(collectionStation_c.getVod_add_time().toString());
        vod_value_add.setText(collectionStation_c.getVod_value_add().toString());
        vod_play_url.setText(collectionStation_c.getVod_play_url().toString());
        vod_pic_url.setText(collectionStation_c.getVod_pic_url().toString());
        LoadImage(vod_pic, collectionStation_c.getVod_pic());


        return view;

    }


    private void LoadImage(ImageView img, String path) {
        //ImageLoader.loadImage(image_url, filmclass.getPimg());
        //Log.d("LoadImage", "img=====" + path);
        AsyncTaskImageLoad async = new AsyncTaskImageLoad(img);
        async.executeOnExecutor(Executors.newCachedThreadPool(), path);//快的多
        //async.execute(path);
    }


    private final class ViewHolder {
        TextView vod_score = null;
        TextView vod_name = null;
        TextView vod_class = null;
        TextView vod_add_time = null;
        ImageView vod_pic = null;
        TextView vod_play_url = null;
        TextView vod_pic_url = null;
        TextView vod_value_add = null;


        public ViewHolder(TextView vod_score, TextView vod_name, TextView vod_class, TextView vod_add_time, TextView vod_value_add, TextView vod_play_url, TextView vod_pic_url, ImageView vod_pic) {
            this.vod_class = vod_class;
            this.vod_score = vod_score;
            this.vod_name = vod_name;
            this.vod_add_time = vod_add_time;
            this.vod_play_url = vod_play_url;
            this.vod_pic_url = vod_pic_url;
            this.vod_value_add = vod_value_add;
            this.vod_pic = vod_pic;

        }

    }
}